﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UserSession
/// </summary>
public class UserSession
{
    public long UserId { get; set; }
    public string Username { get; set; }
    public string FullName { get; set; }
    public long UserGroupId { get; set; }
    public string GroupName { get; set; }
    public long AccountId { get; set; }
    public int LoginType { get; set; }
    public decimal Balance { get; set; }

    public UserSession()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public UserSession(long _userId, string _username, string _fullname, long _userGroupId, string _groupName, long _accountId, int _loginType, decimal _balance)
    {
        UserId = _userId;
        Username = HttpContext.Current.Server.HtmlDecode(_username);
        FullName = HttpContext.Current.Server.HtmlDecode(_fullname);
        UserGroupId = _userGroupId;
        GroupName = HttpContext.Current.Server.HtmlDecode(_groupName);
        AccountId = _accountId;
        LoginType = _loginType;
        Balance = _balance;
    }

    public static void SetSession(UserSession acc)
    {
        try
        {
            HttpContext.Current.Session["User"] = acc;
        }
        catch (Exception ex)
        {
            LogController.WriteLog(ex);
        }
    }

    public static void UnSetSession()
    {
        try
        {
            HttpContext.Current.Session["User"] = null;
        }
        catch (Exception ex)
        {
            LogController.WriteLog(ex);
        }
    }

    public static UserSession GetSession
    {
        get
        {
            try
            {
                if (HttpContext.Current.Session["User"] != null)
                    return (UserSession)HttpContext.Current.Session["User"];
                return null;
            }
            catch (Exception ex)
            {
                LogController.WriteLog(ex);
                return null;
            }
        }
    }

    public static long GetUserId
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                return acc.UserId;
            }
            return 0;
        }
    }

    public static string GetUserName
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                return acc.Username;
            }
            return string.Empty;
        }
    }
    public static string GetFullName
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                return acc.FullName;
            }
            return string.Empty;
        }
    }
    public static long GetUserGroupId
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                return acc.UserGroupId;
            }
            return 0;
        }
    }

    public static string GetGroupName
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                return acc.GroupName;
            }
            return string.Empty;
        }
    }

    public static long GetAccountId
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                return acc.AccountId;
            }
            return 0;
        }
    }

    public static int GetLoginType
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                //GateCardAPI objAPI = new GateCardAPI();
                //UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                //int intResult = 0;

                //Daily_User objUser = objAPI.User_GetUserByUserName(acc.Username, ref intResult);

                //if (intResult != ConstantsAPIUserResult.Successful.Key)
                //{
                //    UnSetSession();
                //    return ConstantsLoginType.PasswordLevel2.Key;
                //}

                //acc.LoginType = objUser.LoginType;
                //UserSession.SetSession(acc);

                //return acc.LoginType;
            }
            return 1;
        }
    }

    public static decimal GetBalance
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                //GateCardAPI objAPI = new GateCardAPI();
                //BLL_Daily_Account objBll = new BLL_Daily_Account();
                //UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                //int intResult = 0;

                //Daily_User objUser = objAPI.User_GetUserByUserName(acc.Username, ref intResult);

                //if (intResult != ConstantsAPIUserResult.Successful.Key)
                //{
                //    UnSetSession();
                //    return ConstantsLoginType.PasswordLevel2.Key;
                //}

                //acc.Balance = objUser.Balance;
                //UserSession.SetBalance = acc.Balance;

                //// Luu balance vao database
                //objBll.UpdateAccountBalance(objUser.UserName, objUser.Balance);

                //return acc.Balance;
            }
            return 0;
        }
    }

    public static decimal SetBalance
    {
        set
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                acc.Balance = value;
                HttpContext.Current.Session["User"] = acc;
            }
        }
    }

    public static bool IsLock
    {
        get
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                //GateCardAPI objAPI = new GateCardAPI();
                //UserSession acc = (UserSession)HttpContext.Current.Session["User"];
                //int intResult = 0;
                //Daily_User objUser = objAPI.User_GetUserByUserName(acc.Username, ref intResult);

                //if (intResult != ConstantsAPIUserResult.Successful.Key)
                //{
                //    UnSetSession();
                //    return true;
                //}

                //bool blnIsLock = objUser.Status != ConstantsUserStatus.Active.Key ? true : false;
                //if (blnIsLock)
                //{
                //    UnSetSession();
                //}

                //return blnIsLock;
            }
            return true;
        }
    }

    #region "Session for new register"
    public static void SetSessionRegisterNewUser(string _username)
    {
        try
        {
            HttpContext.Current.Session["RegisterNewUser"] = _username;
        }
        catch (Exception ex)
        {
            LogController.WriteLog(ex);
        }
    }

    public static void UnSetSessionRegisterNewUser()
    {
        try
        {
            HttpContext.Current.Session["RegisterNewUser"] = null;
        }
        catch (Exception ex)
        {
            LogController.WriteLog(ex);
        }
    }

    public static string GetSessionRegisterNewUser
    {
        get
        {
            if (HttpContext.Current.Session["RegisterNewUser"] != null)
            {
                string str = Convert.ToString(HttpContext.Current.Session["RegisterNewUser"]);
                return str;
            }
            return string.Empty;
        }
    }
    #endregion
}